using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using AjaxControlToolkit;
using System.Web.UI;
using System.Web;
using System.Web.UI.WebControls;
using System.ComponentModel;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;

namespace FAQWebPart
{
    /// <summary>
    /// This WebPart is inspired in the FAQPlaceholder for CMS develop as part of the OTGWeb Web Sample
    /// (more information on this sample in http://www.mcmsfaq.com/cs/faq.asp)
    /// This version has been developed for MOSS. Instead of developing it as a FieldControl I decided to 
    /// develop it as WebPart becuase it seems like a more structured and SharePointish style.
    /// This parts assummes there is a Custom List that has at least a Title Column and Content Column
    /// And it renders its contents using the AjaxToolkit Accordion Control. The FAQ placeholder used an htc but the AJAX
    /// approach seems more contemporary. However remember that you must AjaxEnable your site prior trying to use this webpart.
    /// The documentation for this WebPart describes this process as explained by the SharePoint Guru  Mike Ammerlaan (Integrating ASP.NET AJAX with SharePoint)
    /// This sample also takes advantage of an AjaxBasePart class developed by  Eric Schoonover (AjaxBasePart: Easy ASP.NET 2.0 AJAX Extensions 1.0 and Office SharePoint Server 2007)
    /// that easily allows to create new WebParts with Ajax controls.
    /// Useful links you could check for AjaxEnabling your site are:
    /// http://ajax.asp.net/ 
    /// http://ajax.asp.net/docs/ConfiguringASPNETAJAX.aspx 
    /// http://sharepoint.microsoft.com/blogs/mike/Lists/Posts/Post.aspx?ID=3
    /// 
    /// </summary>
    [DefaultProperty("FAQListName"), ToolboxData("<{0}:FAQWebPart runat=server></{0}:FAQWebPart>"),XmlRoot(Namespace = "FAQWebPart.FAQWebPart")]
    public class FAQWebPart : CapDes.SharePoint.AjaxBasePart
    {

        public FAQWebPart()
        {
            Title = "Frequently Asked Questions";
        }



        /// <summary>
        ///  CreateChildControls
        ///  We override CreateChildControls to add child ASP.NET controls to this Web Part.
        ///  Here we look for the specified FAQList and then we create all the accordion controls
        ///  for the items found on that list
        /// </summary>
        protected override void CreateChildControls()
        {
            //We obtain the context to have access to the SharePoint objects in MOSS
            SPContext spc = SPContext.Current;
            if (spc != null)
            {
                if (FAQListName != null)
                {
                    SPListCollection lists = spc.Web.Lists;
                    //We look for the specified list
                    SPList faq_list = lists[FAQListName];
                    Accordion newAccordion;
                    //If the list is not found or empty we report it
                    if (faq_list == null || faq_list.Items.Count==0)
                    {
                        Literal msg = new Literal();
                        if (faq_list == null)
                            msg.Text = " The list &quot;" + FAQListName + "&quot; could not be found!";
                        else
                            msg.Text = " The list &quot;" + FAQListName + "&quot; has no items!";
                        Controls.Add(msg);
                    }
                    else
                    
                    {
                        //Ok the list has been found and contains data
                        int i = 0;
                        //We iterate thru all the items creating accordions
                        // for each FAQ entry.
                        foreach (SPListItem item in faq_list.Items)
                        {

                            newAccordion = new Accordion();
                            int currentID = i++;
                            newAccordion.ID = "accordion" + currentID;
                            newAccordion.HeaderCssClass = "accordionHeader";
                            newAccordion.ContentCssClass = "accordionContent";
                            newAccordion.FadeTransitions = false;
                            newAccordion.FramesPerSecond = 40;
                            newAccordion.TransitionDuration = 200;
                            newAccordion.AutoSize = AjaxControlToolkit.AutoSize.None;
                            newAccordion.RequireOpenedPane = false;
                            newAccordion.SuppressHeaderPostbacks = true;

                            AccordionPane pane1 = new AccordionPane();
                            //We must give an different id for each pane
                            pane1.ID = "accPane" + currentID;
                            String text = item["Title"].ToString();
                            String contents = item["Content"].ToString();
                            pane1.Header = new SimpleTemplate(text);
                            pane1.Content = new SimpleTemplate(contents);
                            newAccordion.Panes.Add(pane1);
                            //The new accordion is added to the child controls list
                            Controls.Add(newAccordion);
                        }
                    }
                }
            }

        }

        /// <summary>
        /// Render this Web Part to the output parameter specified.
        /// </summary>
        /// <param name="output"> The HTML writer to write out to </param>
        protected override void Render(HtmlTextWriter output)
        {
            // The EnsureChildControls method calls the
            // CreateChildControls method only if the
            // ChildControlsCreated property is false.
            EnsureChildControls();
            
            //This will render all the Accordions
            RenderChildren(output);
        }


        string myFAQListName;


        /// <summary>
        /// This is the property that indicates the WebPart which list to look for.
        /// It will appear in the Miscellaneous category on the ToolPart
        /// </summary>
        [Browsable(true),
         FriendlyName("FAQ List Name"),
         Description("This is the list that will be used to get the data. It must have a title and a content column"),
         WebPartStorage(Storage.Shared)]
        public string FAQListName
        {
            get { return myFAQListName; }
            set { myFAQListName = value; }
        }


    }


    /// <summary>
    /// This class implements a simple ITemplate that holds a literal control. 
    /// It is used to fill the Title and contents of the Accordion.
    /// </summary>
    public class SimpleTemplate : ITemplate
    {
        String text;
        public SimpleTemplate()
        {
            text = "*****";
        }

        public SimpleTemplate(String mytext)
        {
            this.text = mytext;
        }


        //must implement following method
        public void InstantiateIn(Control container)
        {
            LiteralControl l = new LiteralControl();
            l.Text = this.text;
            container.Controls.Add(l);
        }

    }

}

